package com.htu.courseService.picture.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htu.courseService.common.enums.ErrorCodeEnum;
import com.htu.courseService.common.enums.RedisCachePrefix;
import com.htu.courseService.common.exception.BusinessException;
import com.htu.courseService.common.util.MinioUtil;
import com.htu.courseService.picture.dao.FamousTeacherMapper;
import com.htu.courseService.picture.entity.FamousTeacher;
import com.htu.courseService.picture.service.FamousTeacherService;
import com.htu.courseService.picture.vo.FamousTeacherDto;
import com.htu.courseService.user.dao.UserMapper;
import com.htu.courseService.user.vo.UserDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class FamousTeacherServiceImpl implements RedisCachePrefix, FamousTeacherService {
    private final MinioUtil minioUtil;
    private final FamousTeacherMapper famousTeacherMapper;
    private final UserMapper userMapper;

    @Override
    public IPage<FamousTeacherDto> getFamousTeacherList(int pageNum,int pageSize) {
        IPage<FamousTeacherDto> dtoList = famousTeacherMapper.getFamousTeacherList(new Page<>(pageNum,pageSize));
        return dtoList;
    }

    @Override
    public void addFamousTeacherInfo(FamousTeacherDto famousTeacherDto) {
        if(Objects.isNull(famousTeacherDto) || null == famousTeacherDto.getTeacherId()){
            throw new BusinessException(ErrorCodeEnum.USERNAME_NOT_FOUND);
        }
        FamousTeacher famousTeacher = new FamousTeacher();
        famousTeacher.setTeacherId(famousTeacherDto.getTeacherId());
        famousTeacher.setTeacherName(famousTeacherDto.getTeacherName());
        famousTeacher.setFileUrl(famousTeacherDto.getFileUrl());
        famousTeacher.setFileType(famousTeacherDto.getFileName());
        famousTeacher.setFileType(famousTeacherDto.getFileType());
        famousTeacher.setDescription(famousTeacherDto.getDescription());
        famousTeacher.setFileName(famousTeacherDto.getFileName());
        famousTeacherMapper.insert(famousTeacher);
    }

    @Override
    public void deleteFamousTeacher(Integer id) {
        int count = famousTeacherMapper.deleteById(id);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.DELETE_FAIL);
        }
    }

    @Override
    public List<UserDTO> getTeacherInfo(String username) {
        List<UserDTO> userDtoList = userMapper.getTeacherInfo(username);
        return userDtoList;
    }

    @Override
    public FamousTeacherDto addPhoto(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String fileUrl = minioUtil.upload(file);
        String originalFilename = file.getOriginalFilename();
        String fileType = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            fileType =  originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        }
        FamousTeacherDto famousTeacherDto = new FamousTeacherDto();
        famousTeacherDto.setFileUrl(fileUrl);
        famousTeacherDto.setFileName(fileName);
        famousTeacherDto.setFileType(fileType);
        return famousTeacherDto;
    }

    @Override
    public void previewFile(HttpServletResponse response, String fileUrl) {
        minioUtil.preview(fileUrl,response);
    }
}
